import java.util.ArrayList;


public class Ex7_7 {
	
	public String v, w, u;
	private int[][][] s;
	private int[][][] back;
	private String r_v, r_w, r_u;
	
	public Ex7_7() {
		ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\dataset_80_5.txt");
		this.v = inputs.get(0);
		this.w = inputs.get(1);
		this.u = inputs.get(2);
		r_v = "";
		r_w = "";
		r_u = "";
	}
	
	
	public static void main(String[] args) {
		Ex7_7 ex = new Ex7_7();
		ex.getScores();
		ex.getOutputLCS(ex.v.length(), ex.w.length(), ex.u.length());
		System.out.println(ex.s[ex.v.length()][ex.w.length()][ex.u.length()]);
		System.out.println(ex.r_v);
		System.out.println(ex.r_w);
		System.out.println(ex.r_u);		
	}
	
	
	private void getOutputLCS(int i, int j, int k) {
		
		if (i == 0 && j == 0 && k == 0) {
			return;
		}
				
		// {e_ijk,g_ijk,d_ijk,f_ijk,a_ijk,c_ijk,b_ijk};
		if (back[i][j][k] == 0 && j > 0) { 
			getOutputLCS(i,j-1,k);			
			r_v += "-";
			r_w += w.charAt(j-1);
			r_u += "-";
		} else if (back[i][j][k] == 1) {
			getOutputLCS(i-1,j,k);
			r_v += v.charAt(i-1);
			r_w += "-";
			r_u += "-";			
		} else if (back[i][j][k] == 2) {
			getOutputLCS(i,j,k-1);
			r_v += "-";
			r_w += "-";
			r_u += u.charAt(k-1);
		} else if (back[i][j][k] == 3) {
			getOutputLCS(i-1,j-1,k);
			r_v += v.charAt(i-1);
			r_w += w.charAt(j-1);
			r_u += "-";			
		} else if (back[i][j][k] == 4) {
			getOutputLCS(i,j-1,k-1);
			r_v += "-";
			r_w += w.charAt(j-1);
			r_u += u.charAt(k-1);
		} else if (back[i][j][k] == 5) {
			getOutputLCS(i-1,j,k-1);
			r_v += v.charAt(i-1);
			r_w += "-";
			r_u += u.charAt(k-1);
		} else 	{
			getOutputLCS(i-1,j-1,k-1);
			r_v += v.charAt(i-1);
			r_w += w.charAt(j-1);
			r_u += u.charAt(k-1);
		}				
	}	
	
	
	/*
	        B----------C
	       /|        / |
	      / |       /  |  
	    A --|----- D   |
		|   |      |   |
		|  /F------|---G
	    | /       k| /   i
	    |/         |/
	    E--------(i,j,k)
	          j
	*/
	private void getScores() {		
		int n = v.length();
		int m = w.length();
		int l = u.length();		
		s = new int[n+1][m+1][l+1];
		back = new int[n+1][m+1][l+1];		
		for (int j=0;j<=m;j++) {
			s[0][j][0] = 0;
			back[0][j][0] = 0;
		}
		for (int i=0;i<=n;i++) {
			s[i][0][0] = 0;
			back[i][0][0] = 1;
		}
		for (int k=0;k<=l;k++) {
			s[0][0][k] = 0;
			back[0][0][k] = 2;
		}
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=m;j++) {
				s[i][j][0] = 0;
				back[i][j][0] = 3;
			}			
		}
		for (int j=1;j<=m;j++) {
			for (int k=1;k<=l;k++) {
				s[0][j][k] = 0;
				back[0][j][k] = 4;
			}			
		}
		for (int i=1;i<=n;i++) {
			for (int k=1;k<=l;k++) {
				s[i][0][k] = 0;
				back[i][0][k] = 5;
			}			
		}
				
		back[0][0][0] = 6;
		
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=m;j++) {
				for (int k=1;k<=l;k++) {
					// edges
					int e_ijk = s[i][j-1][k];
					int g_ijk = s[i-1][j][k];
					int d_ijk = s[i][j][k-1];
					// diagonals
					int f_ijk = s[i-1][j-1][k];
					int a_ijk = s[i][j-1][k-1];					
					int c_ijk = s[i-1][j][k-1];
					
					int b_ijk = s[i-1][j-1][k-1];
					
					if (v.charAt(i-1) == w.charAt(j-1) 
									&& v.charAt(i-1) == u.charAt(k-1)) {
						b_ijk += 1;
					}					
					Integer[] list = {e_ijk,g_ijk,d_ijk,f_ijk,a_ijk,c_ijk,b_ijk};										
					
					s[i][j][k] = Utils.max(list);
					back[i][j][k] = Utils.maxIndex(list);
					list = null;
				}
			}
		}			
	}
	
} // end class
